//Components.utils.import("resource://gre/modules/FileUtils.jsm");
var ulttool={
	init:function(){
//		Components.utils.import("resource://gre/modules/FileUtils.jsm");
		ulttool.Cc = Components.classes;
		ulttool.Ci = Components.interfaces;
		ulttool.console = ulttool.Cc["@mozilla.org/consoleservice;1"].getService(ulttool.Ci.nsIConsoleService);
		ulttool.scriptloader= ulttool.Cc["@mozilla.org/moz/jssubscript-loader;1"].getService(ulttool.Ci.mozIJSSubScriptLoader);
		ulttool.ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
		ulttool.ResProt = ulttool.ioService.getProtocolHandler("resource").QueryInterface(ulttool.Ci.nsIResProtocolHandler);
		ulttool.ResProt.setSubstitution('antiyouku', ulttool.ioService.newURI("chrome://antiyouku/content/player/", null, null));
	},
	loginfo:function(infostr){
		ulttool.console.logStringMessage(infostr);
	},
	loadsubScript:function(scriptpath){
		ulttool.scriptloader.loadSubScript("chrome://antiyouku/content/ChannelReplacement.js");
		ulttool.scriptloader.loadSubScript("chrome://antiyouku/content/UltIo.js");
	},
	CCIN:function (cName, ifaceName) {
    	return ulttool.Cc[cName].createInstance(ulttool.Ci[ifaceName]);
	},
	creatDir:function(dir){
		var newdir = FileUtils.getDir("ProfD", [dir], true);
		return newdir;
	},
	loadfile:function(name,charset){
		var res;
//		var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
		var file = Components.classes["@mozilla.org/file/directory_service;1"].
           getService(Components.interfaces.nsIProperties).
           get("ProfD", Components.interfaces.nsIFile);
//		file.initWithPath(path);
		file.append('antiyouku');
		file.append(name);
        var is = ulttool.Cc["@mozilla.org/network/file-input-stream;1"].createInstance(ulttool.Ci.nsIFileInputStream);
        is.init(file, 0x01, 256, null);
        var sis = ulttool.Cc["@mozilla.org/scriptableinputstream;1"].createInstance(ulttool.Ci.nsIScriptableInputStream);
        sis.init(is);

        res = sis.read(sis.available());
        is.close();
        
        if (charset !== null) { // use "null" if you want uncoverted data...
            var unicodeConverter = ulttool.Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(ulttool.Ci.nsIScriptableUnicodeConverter);
            try {
                unicodeConverter.charset = charset || "UTF-8";
            } catch(ex) {
                unicodeConverter.charset = "UTF-8";
            }
            res = unicodeConverter.ConvertToUnicode(res);
        }
        return res;
	},
	
	savetxtfile:function (name,data){
		var file = Components.classes["@mozilla.org/file/directory_service;1"].
			getService(Components.interfaces.nsIProperties).
			get("ProfD", Components.interfaces.nsIFile);
		file.append("antiyouku'");
		file.append(name);
		var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].
		   createInstance(Components.interfaces.nsIFileOutputStream);
		foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0); 
		var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
		    createInstance(Components.interfaces.nsIConverterOutputStream);
		converter.init(foStream, "UTF-8", 0, 0);
		converter.writeString(data);
		converter.close(); // this closes foStream
	},
	loadbytefile:function (name){
		var res;
//		var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
		var file = Components.classes["@mozilla.org/file/directory_service;1"].
           getService(Components.interfaces.nsIProperties).
           get("ProfD", Components.interfaces.nsIFile);
//		file.initWithPath(path);
		file.append('antiyouku');
		file.append(name);
        var is = ulttool.Cc["@mozilla.org/network/file-input-stream;1"].createInstance(ulttool.Ci.nsIFileInputStream);
        is.init(file, -1, -1, false);
        var bstream = Components.classes["@mozilla.org/binaryinputstream;1"].
              createInstance(Components.interfaces.nsIBinaryInputStream);
		bstream.setInputStream(is);

        res = bstream.readBytes(bstream.available());
        is.close();
        return res;
	},
	savebytefile:function (name,bytedata){
		var file = Components.classes["@mozilla.org/file/directory_service;1"].
			getService(Components.interfaces.nsIProperties).
			get("ProfD", Components.interfaces.nsIFile);
			file.append("antiyouku'");
			file.append(name);
			
		var stream = Components.classes["@mozilla.org/network/safe-file-output-stream;1"].
        	createInstance(Components.interfaces.nsIFileOutputStream);
		stream.init(aFile, 0x04 | 0x08 | 0x20, 0600, 0); // readwrite, create, truncate           
		stream.write(bytedata, bytedata.length);
		if (stream instanceof Components.interfaces.nsISafeOutputStream) {
		    stream.finish();
		} else {
		    stream.close();
		}    
	}
};

ulttool.init();